package easycloud.ecs.web.controller;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import easycloud.ecs.common.domain.Sync;
import easycloud.ecs.common.domain.User;
import easycloud.ecs.web.exception.DomainNotFoundException;
import easycloud.ecs.web.service.SyncService;

@Controller
@RequestMapping("sync")
public class SyncController extends BaseController {

	private static final Log logger = LogFactory.getLog(SyncController.class);

	@Inject
	private SyncService syncService;

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public ModelAndView createProcess(@ModelAttribute("sync") @Valid Sync sync,
			BindingResult syncBindingResult, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("common.msg");
		if (syncBindingResult.hasErrors()) {
			mav.setViewName("sync.create");
			return mav;
		}
		try {
			syncService.save(sync);
			mav.addObject("msg", "保存成功");
		} catch (Exception e) {
			mav.addObject("msg", "保存失败");
		}
		return mav;
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public ModelAndView createSetup(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("sync.create");
		Sync sync = new Sync();
		User user = getUserWithSession(request);
		mav.addObject("sync", sync);
		mav.addObject("providers", user.getProviders());
		mav.addObject("folders", user.getFolders());
		return mav;
	}

	@RequestMapping(value = "delete/{syncId}", method = RequestMethod.GET)
	public ModelAndView deleteProcess(@PathVariable("syncId") Integer syncId,
			HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("common.msg");
		Sync sync = syncService.getSync(syncId);
		User user = getUser(request);
		try {
			if (sync.getFolder().getUser().equals(user)) {
				syncService.delete(sync);
			}
			mav.addObject("msg", "删除成功");
		} catch (Exception e) {
			mav.addObject("msg", "删除失败");
		}
		return mav;
	}

	@RequestMapping(value = "{syncId}")
	public ModelAndView detail(@PathVariable("syncId") Integer syncId) {
		ModelAndView mav = new ModelAndView("sync.detail");
		Sync sync = syncService.getSync(syncId);
		if (sync == null) {
			throw new DomainNotFoundException(Sync.class, syncId);
		}

		mav.addObject("sync", sync);
		return mav;
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView listSetup(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("sync.list");
		User user = getUser(request);
		List<Sync> syncs = syncService.findSync(user);
		mav.addObject("syncs", syncs);
		return mav;
	}
}
